define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'order/index' + location.search,
                    add_url: 'order/add',
                    edit_url: 'order/edit',
                    del_url: 'order/del',
                    multi_url: 'order/multi',
                    import_url: 'order/import',
                    export_url: 'order/export',
                    table: 'order',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'order_no', title: __('Order_no'), operate: 'LIKE'},
                        {field: 'product_title', title: __('产品名称'), operate: 'LIKE'},
                        {field: 'num', title: __('Num'), operate: 'BETWEEN'},
                        {field: 'price', title: __('Price'), operate: 'BETWEEN'},
                        {field: 'actual_price', title: __('Actual_price'), operate: 'BETWEEN'},
                        {
                            field: 'nickname',
                            title: __('Nickname'),
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.content
                        },
                        {field: 'mobile', title: __('Mobile'), operate: 'LIKE'},
                        {
                            field: 'address',
                            title: __('Address'),
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.content
                        },

                        {
                            field: 'pay_type', title: __('Pay_type'), formatter: function (value, row, index) {
                                // Define different behaviors based on the numeric value
                                switch (value) {
                                    case 0:
                                        return '待支付';
                                    case 1:
                                        return '已完成';
                                }
                            },
                            searchList: {0: '待支付', 1: '已完成'},
                        },
                        {
                            field: 'pay_time',
                            title: __('Pay_time'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false,
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'status', title: __('Status'), formatter: function (value, row, index) {
                                // Define different behaviors based on the numeric value
                                switch (value) {
                                    case 0:
                                        return '进行中';
                                    case 2:
                                        return '已完成';
                                }
                            },
                            searchList: {0: '进行中', 1: '已完成'},
                        },
                        {
                            field: 'confirm_time',
                            title: __('Confirm_time'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false,
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'createtime',
                            title: __('下单时间'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false,
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});

$('#exportButton').click(function () {
    var m = '确定导出根据条件的全部数据吗？';
    /*获取搜索条件*/
    var where = {}, op = {}, data = {};
    var form_data = $('.commonsearch-table form').serializeArray();
    var i_name;
    //获取搜索条件
    $(form_data).each(function () {
        var index = this.name.indexOf('-operate')
        if (index > 0) {
            i_name = this.name.substring(0, index);
            op[i_name] = this.value;
        } else {
            if (this.value != '') {
                i_name = this.name;
                where[i_name] = this.value;
            }
        }
    })

    data.filter = JSON.stringify(where);
    data.op = JSON.stringify(op);

    Layer.confirm(m, function (index) {
        Layer.close(index);
        Fast.api.ajax_get({
            url: "order/export",
            data: data,
        }, function (data, ret) {
            // console.log(ret)
            //直接跳转到文件 输出下载
            window.location.href = ret.url;
        });
    });
    return false;
})
